/**
 * Copyright (C) 2016 Eshore Corporation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.eshore.thrpc.server.thrift;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eshore.thrpc.server.IServer;

/**
 * Thrift服务
 * <p>
 * 
 * @author : zhouzhixiang
 * @date : 2016-11-13
 */
public class ThriftServer implements IServer {

    /** LOGGER */
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    /** 服务线程 */
    private TServerThread serverThread;

    /** 是否已经启动 */
    private boolean isStarted = false;
    
    
    @Override
    public void start() {
        if (!isStarted) {
            serverThread.start();
            isStarted = serverThread.isStarted();
        }
    }

    @Override
    public void stop() {
        if (isStarted) {
            serverThread.stopServer();
            isStarted = false;
        }
    }

    @Override
    public boolean isStarted() {
        int i = 0;
        while (!isStarted && i < 3) {
            isStarted = serverThread.isStarted();
            try {
                Thread.sleep(100 * i++);
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage(), e);
            }
        }
        return isStarted;
    }

	public TServerThread getServerThread() {
		return serverThread;
	}

	public void setServerThread(TServerThread serverThread) {
		this.serverThread = serverThread;
	}
}
